package com.jie.deletefile.Doahang;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jie.deletefile.R;

import java.util.ArrayList;
import java.util.List;

public class WeixinActivity extends FragmentActivity {

    private ViewPager vp1;
    private RadioButton rbChat;
    private RadioButton rbAddress;
    private RadioButton rbFind;
    private RadioButton rbMe;
    private RadioGroup tab_menu;
    private MianFragment mianFragment;
    private TongxunFragment tongxunFragment;
    private FaxianFragment faxianFragment;
    private WoFragment woFragment;
    private List<Fragment> data;
    private float width;
    private ImageView view;
    private float startOffset;
    private float stopOffset;


    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weixin);
        initView();
        initFragment();
        startOffset = 0;
        adapter = new MyAdapter(getSupportFragmentManager());
        vp1.setAdapter(adapter);
        vp1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                startOffset = (width * position)/4;
                stopOffset = startOffset + positionOffsetPixels/4;
                startOffset = stopOffset;
                Log.e("TAG", "onPageScrolled: start" + startOffset);
                Log.e("TAG", "onPageScrolled: stop"+stopOffset);
                Log.e("TAG", "onPageScrolled: postion"+position );
                new ObjectAnimator().ofFloat(view, "translationX", startOffset, stopOffset).setDuration(200).start();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void initFragment() {
        woFragment = new WoFragment();
        faxianFragment = new FaxianFragment();
        tongxunFragment = new TongxunFragment();
        mianFragment = new MianFragment();
        data = new ArrayList<>();
        data.add(mianFragment);
        data.add(tongxunFragment);
        data.add(faxianFragment);
        data.add(woFragment);

    }

    private void initView() {
        vp1 = (ViewPager) findViewById(R.id.vp1);
        rbChat = (RadioButton) findViewById(R.id.rbChat);
        rbAddress = (RadioButton) findViewById(R.id.rbAddress);
        rbFind = (RadioButton) findViewById(R.id.rbFind);
        rbMe = (RadioButton) findViewById(R.id.rbMe);
        tab_menu = (RadioGroup) findViewById(R.id.tab_menu);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;

        view = (ImageView) findViewById(R.id.v);
        view.setMinimumWidth((int) (width / 4));
    }

    class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }

        @Override
        public int getCount() {
            return data.size();
        }
    }


}

